<?php

/**
 * Beaver_Language
 * 
 * @category   Beaver
 * @package    Beaver
 * @author     Mihály Kuprivecz
 */
class Beaver_Language {
	
	/**
	 * Container of the Beaver_Language instance 
	 *
	 * @var Beaver_Language
	 */
	private static $instance = null;
	
	/**
	 * Language state
	 *
	 * @var string
	 */
	private $_language = "hu";
	
	/**
	 * Container of registred language files
	 *
	 * @var array
	 */
	private $_languageFiles = array ();
	
	/**
	 * Container of registred language file paths
	 *
	 * @var array
	 */
	private $_languagePaths = array ();
	
	/**
	 * Container of the available language strings
	 *
	 * @var array
	 */
	private $_strings = array();
	
	/**
	 * Private, because the singleton pattern not instantiatable
	 */
	private function __construct() {
		$this->registerLanguagePath ( dirname ( __FILE__ ) . DIRECTORY_SEPARATOR . "Language" );
		$this->registerLanguageFile ( "Beaver" );
		$this->setLanguage ( $this->_language );
	}
	
	/**
	 * Get the instance of Beaver_Language
	 *
	 * @return Beaver_Language
	 */
	public function getInstance() {
		if (null === self::$instance) {
			self::$instance = new self ( );
		}
		return self::$instance;
	}
	
	/**
	 * Can't clone the singleton pattern ionstance
	 */
	public final function __clone() {
		throw new Beaver_Exception ( "Cannot clone instance of Beaver_Language" );
	}
	
	/**
	 * Set the language
	 *
	 * @return Beaver_Language
	 */
	public function setLanguage($language) {
		$this->_language = $language;
		return $this;
	}
	
	/**
	 * Get the language state
	 *
	 * @return string
	 */
	public function getLanguage() {
		return $this->_language;
	}
	
	public function registerLanguagePath($path) {
		$this->_languagePaths [] = $path;
		return $this;
	}
		
	public function registerLanguageFile($name) {
		$this->_languageFiles [] = $name;
		return $this;
	}
	
	public function loadLanguage() {
		$this->_strings = array();
		for($i = 0; $i < count ( $this->_languagePaths ); $i ++) {
			foreach ( $this->_languageFiles as $name ) {
				$file = $this->_languagePaths [$i] . DIRECTORY_SEPARATOR . $name . "." . $this->_language . ".ini";
				if (file_exists ( $file )) {
					if (isset($this->_strings [$name]) && is_array($this->_strings [$name]))
					{
						$this->_strings [$name] = array_merge($this->_strings [$name], parse_ini_file ( $file ));	
					}
					else
					{
						$this->_strings [$name] = parse_ini_file ( $file );
					}
				}
			}
		}
	}
	
	public function get($tag, $name, $parameters = null) {
		if ($this->_strings == null) {
			$this->loadLanguage ();
		}
		$name = utf8_decode($name);
		$tag  = utf8_decode($tag);
		if ( isset($this->_strings [$tag] [$name]))
		{
			$string = $this->_strings [$tag] [$name];
		}
		else 
		{
			$string = "undefined";
		}
		if (null !== $parameters && is_array ( $parameters )) {
			$valueArray = array ();
			$searchArray = array ();
			foreach ( $parameters as $name => $value ) {
				$valueArray [] = $value;
				$searchArray [] = "{" . $name . "}";
			}
			$string = str_replace ( $searchArray, $valueArray, $string );
		}
		return $string;
	}
}